package com.example.graduatedesigned.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import android.content.Intent;
import android.os.Bundle;
import com.example.graduatedesigned.R;
import com.example.graduatedesigned.adapter.FragmentAdapter;
import com.example.graduatedesigned.fragment.ChartFragment;
import com.example.graduatedesigned.fragment.HistoryFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HistoryCharts extends AppCompatActivity {
    private static List<Fragment> fragmentList;
    private static List<String> mTitles;
    private static final String[] title = {"列表展示", "图标展示"};
    private FragmentAdapter fragmentAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_charts);
        Intent intent = getIntent();
        String selected = intent.getStringExtra("selected");
        ViewPager2 pager = findViewById(R.id.pager);
        TabLayout tabLayout = findViewById(R.id.tabLayout);
        fragmentList = new ArrayList<>();
        mTitles = new ArrayList<>();
        mTitles.addAll(Arrays.asList(title));
        fragmentList.add(HistoryFragment.newInstance(selected));
        fragmentList.add(ChartFragment.newInstance(selected));
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), this.getLifecycle(),
                mTitles, fragmentList);
        pager.setAdapter(fragmentAdapter);
        new TabLayoutMediator(tabLayout, pager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(mTitles.get(position));
            }
        }).attach();
    }
}